﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CorsatAdminApp.Library.TempTaskClasses;

namespace CorsatAdminApp.Desktop.UserControls
{
    public partial class ucAbscence : UserControl
    {
        public ucAbscence()
        {
            InitializeComponent();
        }

        private void ucAbscence_Load(object sender, EventArgs e)
        {
            GridVeiwFunction();

            CourseTitlesCB.DataSource = Ab.GetAllCourses();
            CourseTitlesCB.DisplayMember = "ItemName";
            CourseTitlesCB.ValueMember = "ItemID";
        }

        #region private Variable

        Absence_Administration Ab = new Absence_Administration();
        int CourseTitleId;
        int CourseLevel;
        int year = DateTime.Now.Year;
        int DataGridCount;

        #endregion

        #region private Methods

        private void GridVeiwFunction()
        {
            try
            {

                AbsenceDataG.Columns.Add(new DataGridViewLinkColumn
                {
                    CellTemplate = new DataGridViewLinkCell(),
                    HeaderText = "م",

                });
                AbsenceDataG.Columns.Add(new DataGridViewLinkColumn
                {
                    CellTemplate = new DataGridViewLinkCell(),
                    HeaderText = "الاسم",

                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_1",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_2",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_3",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_4",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_5",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_6",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_7",
                });
            }
            catch (Exception)
            { MessageBox.Show("Error"); }
        }


        #endregion

     

        private void loadBt_Click_1(object sender, EventArgs e)
        {
            Ab.Absence.Clear();

            //// Add Test Rows to the DataTable
            //Ab.Absence.Rows.Add(1, "Mariam", false, false, false, false, false, false, false);
            //Ab.Absence.Rows.Add(2, "joe", false, false, false, false, true, false, false);
            //Ab.Absence.Rows.Add(3, "koo", false, false, true, false, false, false, false);

            // ---------------------------------------------------------------------------------
            try
            {
              
                CourseTitleId = Convert.ToInt32(CourseTitlesCB.SelectedValue);
                CourseLevel = Convert.ToInt32(levelCB.SelectedValue);

                // Data Grid
                AbsenceDataG.DataSource = null;
                AbsenceDataG.Rows.Clear();
                AbsenceDataG.Columns.Clear();

                // Set the Data Source of the Grid to the DataTable from the class.
                Ab = new Absence_Administration(CourseTitleId, CourseLevel, year);
                AbsenceDataG.DataSource = Ab.Absence;
              

                //loadBt.Enabled = false; // ?
                //SaveBt.Enabled = true;
            }
            catch (Exception) { MessageBox.Show("Error !"); }
        }

        private void SaveBt_Click_1(object sender, EventArgs e)
        {
        
            try
            {
                // Set the Course Title.
                // Note: Set the whole ITEM.
                Ab.CourseTitle.ItemName = Convert.ToString(CourseTitlesCB.SelectedText);
                Ab.CourseTitle.ItemId = Convert.ToInt32(CourseTitlesCB.SelectedValue);
           
                // Set the Course Level.
                Ab.CourseLevel = Convert.ToInt32(levelCB.SelectedValue);

                // Catch the Number of Rows to loop on.
                

                AbsenceDataG.DataSource = null;

                for (int i = 0; i < AbsenceDataG.Rows.Count; i++)
                {
                    for (int j = 0; j < AbsenceDataG.Columns.Count; j++)
                    {
                        // Add the Rows to Save.
                      
                           Ab.Absence.Rows[i]["AccountId"]= Convert.ToInt32(AbsenceDataG.Rows[i].Cells[j].Value); //ID
                           Ab.Absence.Rows[i]["ArabicName"] =Convert.ToString(AbsenceDataG.Rows[i].Cells[j].Value);  // Full Name
                           Ab.Absence.Rows[i]["Att_1"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 1 (Att_1)
                           Ab.Absence.Rows[i]["Att_2"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 2 (Att_2)
                           Ab.Absence.Rows[i]["Att_3"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 3 (Att_3)
                           Ab.Absence.Rows[i]["Att_4"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 4 (Att_4)
                           Ab.Absence.Rows[i]["Att_5"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 5 (Att_5)
                           Ab.Absence.Rows[i]["Att_6"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 6 (Att_6)
                           Ab.Absence.Rows[i]["Att_7"] = Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value); // Day 7 (Att_7)
                    }
                }

                Ab.Save();
               
            }
            catch (Exception) { MessageBox.Show("Please,Select CourseTitle & Level be4 Save ur Data"); }
      
        }

        private void CourseTitlesCB_SelectedIndexChanged(object sender, EventArgs e)
        {

            ////loadBt.Enabled = true;
            ////SaveBt.Enabled = false;


            //AbsenceDataG.DataSource = null;
            //AbsenceDataG.Rows.Clear();
            //AbsenceDataG.Columns.Clear();

            //GridVeiwFunction();
        }
        
    }
}
